package com.zlx.store.goods.config;

import com.zlx.store.goods.job.ExpiredGoodsJob;
import com.zlx.store.goods.job.HotGoodsJob;
import com.zlx.store.goods.job.WarmUpJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.zlx.store.common.constant.CronExpression.EVERY_DAY;

@Configuration
public class GoodsJobConfig {
    /**
     * 店铺种类预热
     */
    @Bean
    public JobDetail WarmUpJob(){
        return JobBuilder.newJob(WarmUpJob.class)
                .storeDurably(true)
                .build();
    }
    @Bean
    public Trigger WarmUpTrigger(){
        return TriggerBuilder.newTrigger()
                .forJob(WarmUpJob())
                .withIdentity("WarmUpTrigger")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(10)
                        .withRepeatCount(0)) // Set repeat count to 0 to run only once after the delay
                .build();
    }

    /**
     * 每日热销商品生成
     * @return
     */
    @Bean
    public JobDetail hotGoodsJob(){
        return JobBuilder.newJob(HotGoodsJob.class)
                .storeDurably(true)
                .build();
    }
    @Bean
    public Trigger hotGoodsTrigger(){
        return TriggerBuilder.newTrigger()
                .forJob(hotGoodsJob())
                .withSchedule(CronScheduleBuilder.cronSchedule( EVERY_DAY.getMsg()))
                .build();
    }
    /**
     * 每日临期商品生成
     * @return
     */
    @Bean
    public JobDetail expiredGoodsJob(){
        return JobBuilder.newJob(ExpiredGoodsJob.class)
                .storeDurably(true)
                .build();
    }
    @Bean
    public Trigger expiredGoodsTrigger(){
        return TriggerBuilder.newTrigger()
                .forJob(expiredGoodsJob())
                .withSchedule(CronScheduleBuilder.cronSchedule(EVERY_DAY.getMsg()))
                .build();
    }
}
